package com.zeroone.star.publish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeroone.star.project.dto.PageDTO;
import com.zeroone.star.publish.entity.Volunteer;
import com.zeroone.star.publish.mapper.VolunteerMapper;
import com.zeroone.star.publish.service.IVolunteerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.mapstruct.Mapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author car
 * @since 2024-03-02
 */

@Mapper(componentModel = "spring")
interface MyVolunteerMapper {

}

@Service
public class VolunteerServiceImpl extends ServiceImpl<VolunteerMapper, Volunteer> implements IVolunteerService {

    @Resource
    private VolunteerMapper volunteerMapper;

    @Override
    public Integer addVolunteer(Volunteer volunteer) {
        volunteer.setPublishTime(String.valueOf(new Date().getTime()));
        return volunteerMapper.insert(volunteer);
    }

    @Override
    public Volunteer modifyVolunteer(Volunteer volunteer) {
        int i = volunteerMapper.updateById(volunteer);
        if (i == 1) {
            return volunteer;
        } else
            return null;
    }

    @Override
    public Integer deleteVolunteer(Integer id) {
        QueryWrapper<Volunteer> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return volunteerMapper.delete(wrapper);
    }


    @Override
    public Volunteer selectVolunteerById(Integer id) {
        return volunteerMapper.selectById(id);
    }

    @Override
    public List<Volunteer> selectAllVolunteer() {
        return volunteerMapper.selectList(new QueryWrapper<>(null));
    }

    @Override
    public List<Volunteer> selectOwnAllVolunteer(String uid) {
        return volunteerMapper.selectList(new QueryWrapper<Volunteer>().eq("user_id", uid));
    }

    @Override
    public IPage<Volunteer> selectByPageLikeContent(String content, Integer pageIndex) {
        Page<Volunteer> page = new Page<>(pageIndex,10);
        QueryWrapper<Volunteer> wrapper = new QueryWrapper<>();
        wrapper.like("content",content);
        return volunteerMapper.selectPage(page,wrapper);
    }


}
